package com.pw.study.arithmetic

import java.io.FileOutputStream
import java.nio.charset.StandardCharsets
import java.util.Random

import scala.collection.mutable.ListBuffer

/**
 * @Desc: 稀疏索引表达
 * @author: future
 * @since: 2022/5/22-12:02
 */
object SparseArr {

  def createArr(): Array[Array[Int]] = {
    val rd = new Random()
    val array: Array[Array[Int]] = Array.ofDim[Int](16, 16)
    for (i <- 0 until 30) {
      val x: Int = rd.nextInt(16)
      val y: Int = rd.nextInt(10) + 6
      val v: Int = rd.nextInt(2) + 1
      array(x)(y)=v
      println(s"($x,$y,$v)")
    }
    array
  }

  def main(args: Array[String]): Unit = {
    val arr: Array[Array[Int]] = createArr()

    for (x <- arr) {
      for (elem <- x) {
        print(elem+" ")
      }
      println()
    }
    var list = ListBuffer[MapNode]()
    for(x <-0 until(arr.length)){
      for(y<-0 until(arr(x).length)){
        val node = MapNode(x, y, arr(x)(y))
        list.append(node)
        //list.+=(node)
      }
    }
    val fileName = "data/file/node.data"
    val out = new FileOutputStream(fileName)
    list.foreach(line =>{
      if(line.value!=0){
        out.write((line+"\r\n").getBytes(StandardCharsets.UTF_8))
        println("line: "+line)
      }
    })
    out.flush()
    out.close()

  }
}

case class MapNode(x: Int, y: Int, value: Int)
